//
//  VOL.m
//  AppFinance
//
//  Created by wei on 12-7-13.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "AFVOL.h"

@implementation AFVOL

static AFVOL *_vol = nil;

static float _kpiMaxVolume = 0;

static float _kpiLastVolume = 0;
static float _kpiLastVolumeM5 = 0;
static float _kpiLastVolumeM10 = 0;

+(AFVOL*) app{
    if (_vol == nil) {
        _vol = [[self alloc] init];
    }
    return _vol;
}

+(float) kpiMaxVolume{
    return _kpiMaxVolume;
}

+(float) kpiLastVolume{
    return _kpiLastVolume;
}
+(float) kpiLastVolumeM5{
    return _kpiLastVolumeM5;
}
+(float) kpiLastVolumeM10{
    return _kpiLastVolumeM10;
}

int vol_iParam[] = {5,10};

-(void) initKlineArray:(NSArray *)_kLineArray{
    kLineArray = _kLineArray;
    
    _kpiMaxVolume = 0;
    _kpiLastVolume = 0;
    _kpiLastVolumeM5 = 0;
    _kpiLastVolumeM10 = 0;
    
    voldata = nil;
    voldata = [NSMutableArray array];
}

-(float) maxVolume:(int)_drawCount{
    
    float m_max = 0.0;
    
    float volume = 0;
    float open = 0;
    float close = 0;
    
    NSString *stauts;
    
    NSDictionary *dict;
    NSMutableDictionary *mdict;
    
    int count = (int)[kLineArray count];
    for(int i = 0; i < count; i++){
        dict = [kLineArray objectAtIndex:i];
        open = [[dict objectForKey:AF_K_MARKET_OPEN] floatValue];
        close = [[dict objectForKey:AF_K_MARKET_CLOSE] floatValue];
        volume = [[dict objectForKey:AF_K_MARKET_VOLUME] floatValue];
        
        // [S]走高 走底 判断
        if (close >= open) {
            stauts = @"up"; //up/down
        } else {
            stauts = @"down"; //up/down
        }
        // [E]
        
        if(volume > m_max){
            m_max = volume;
        }
        
        mdict = [NSMutableDictionary dictionary];
        [mdict setObject:stauts forKey:AF_K_MARKET_STATUS];
        [mdict setObject:[NSNumber numberWithFloat:volume] forKey:AF_K_MARKET_VOLUME];
        
        //NSLog(@" volume = %f",volume);
        [voldata addObject:mdict];
        
        // LAST Volume
        if (i == ([kLineArray count] - 1)) {
            _kpiLastVolume = volume;
        }
    }
    
    _kpiMaxVolume = m_max;
    return m_max;
}

-(NSMutableArray *) volBarData{
    return voldata;
}

-(NSMutableArray *) volLineData:(int) day{
    
    NSLog(@" vol 5 start");
    if ([voldata count] == 0) {
        return nil;
    }
    if ([voldata count] <= day) {
        return nil;
    }
    
    NSMutableArray *array = [NSMutableArray array];
    
    float sum = 0;
    float f = 0;
    
    NSDictionary *dict;
    
    float data = 0;
    for(int i = 0; i < [voldata count]; i++) {
        dict = [voldata objectAtIndex:i];
        f = [[dict objectForKey:AF_K_MARKET_VOLUME] floatValue];
        
        sum += f;
        
        if (i < day) {
            data = sum / day;
        } else {
            dict = [kLineArray objectAtIndex:(i - day)];
            f = [[dict objectForKey:AF_K_MARKET_VOLUME] floatValue];
            
            sum = sum - f;
            data = sum / day;
        }
        
        [array addObject: [NSNumber numberWithFloat:data]];
    }
    NSLog(@" vol 5 end");
    return array;
}
@end
